My journey of C has begun [3]

  • 2020-04-01 21:27:49
  • OfStack

7. The third C procedure
First look at the following little program, try to compile and run it yourself. If you don't know how to compile, please click the following hyperlink:
          (link: http://cpp.ga-la.com/html/1/15/0510/15.htm)
      (link: http://cpp.ga-la.com/html/1/15/0510/12.htm)

         

          # include < stdio.h >

          Int main (void)
          {
                  Float the radius;      

                  Printf ("Please enter the radius: ");
                 
                  The scanf (" % f ", & the radius);
                 
                  Printf ("The area of The circle is: %.3f\n", 3.14 * radius * radius);

                  Printf ("Press ENTER to quit...") );
                  Getchar ();
                  Getchar ();        
                  Return 0;
          }

          "Enter the radius" means: enter the radius of the circle, then press enter. The program asks us to enter Numbers, such as 15, 31.6. Do not enter letters such as ABC, t156. Typing letters will cause an error! We'll learn how to handle this later, but for now, enter the Numbers honestly. Of course, you can also try typing letters and see what happens.

        Now let's go over the details of this program.

        1. Line 7 of the program, we use float A radius is declared variable . float The data type represented is floating-point That's the decimal. In previous programs, we used int To declare variables. An int representing The integer Which is an integer. Variables declared with int can only store integers; Variables declared with float can store decimals.

        2. In order to accept user input, we need to use The scanf Function. and printf Again, scanf is a function defined in the standard library, and we call such a function Standard functions . Like printf, scanf's function prototype is also located Standard header file Stdio.h. A placeholder % f The scanf function reads floating point Numbers. & the radius Tells scanf to give the variable radius with the floating-point number it reads The assignment . &is essential, otherwise the program will fail. & In this case, Address operator , used to get the variable radius Memory address , which tells scanf to store the read floating-point number at that address Memory space , which achieves the purpose of assigning radius. If you don't understand the above, it doesn't matter, really, as long as you can remember it. In the future, we will study these knowledge more deeply.

        3. Radius is a floating point variable , 3.14 by default Double precision floating point ( A double ) constant , so the result of the expression 3.14 * radius * radius is a double-precision floating point number. It doesn't matter if you don't understand it here, we'll study it in more detail later.

        4. To display floating point data, we need to use placeholders % f . % d Is used to display integers, if we put in the second printf %. 3 f Replace it with %d, and the output will be abnormal. 3. Is to tell printf to print only three decimal places. We could change that 2. or . 0 And so on. .0 means no decimal part is output.

        5. The program ended up using two in a row getchar The return () function waits for the user to enter enter before exiting the program. getchar Is also Standard functions , its function prototype is also located in the standard header file stdio.h, which is used to read a character entered by the user. We'll talk more about why we use two getchar here.

8. Errors and warnings

 

      So far, we have written several C programs. Maybe some people compile a program and the compiler says there's a program there error ( The error ) and does not compile programs into executable files. Compiler error indicates that there is an error in our program! Missing semicolon (;) Or not writing curly braces} and so on will cause the compiler to report an error.

        Maybe some people write programs that compile, but the compiler also gives you some warning ( warning ). Compiler alerts indicate that we are writing code that conforms to C syntax, but that may not work as expected.

        As programmers, we should read the error messages and warnings carefully. From that information, we can learn about the program The error to party Where to correct the error. Correction, If you don't find an error on the line that the compiler says went wrong, you need to look up Whether there is an error in a row .

Constants and variables

 

        The value of some data has been determined before the program runs, and cannot be changed during the program runs, such data is called constant Or, constant . For example: 'a' is Character constants , "a" is String constant , 123 is Integer constants , 123.45 is double Floating point constant . The values of the above data are determined before the program runs and cannot be changed, so they are all constant.

      The data that can be changed while the program is running is called variable . Such as:
                    Double PI = 3.14;
In the above statement, PI is a variable, because we can constantly change the value of PI after this statement. Such as:
                  PI = 3.1415;
                  The scanf (" % lf ", & PI);
Both statements can change the value of PI. In the code above, both 3.14 and 3.1415 are double-precision floating point constants. % lf Is the l is Lowercase L Always use lowercase here. What %lf does is tell scanf that the &pi function is one Double precision floating point pointer (double *). And %Lf means, and PI means Extended double precision floating point pointer (long double *). Since PI is a double-precision floating point variable, & PI is a double-precision floating point pointer, so we should use % lf .

        constant and variable The difference is that the value of a constant is constant, while the value of a variable can be changed by, for example, assignment.


Related articles: